//
//  GCMLXKHViewController.m
//  GeeCityManager
//
//  Created by 聚城科技 on 16/3/23.
//  Copyright © 2016年 聚城科技. All rights reserved.
//

#import "GCMLXKHViewController.h"
#import "GCMAddNewLXJLViewController.h"
#import "GCMLXKHTableViewCell.h"
#import "GCMLXKHModel.h"

@interface GCMLXKHViewController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation GCMLXKHViewController{

    UITableView *_tableV;
    NSMutableArray *_dataSource;
    NSInteger _currentPage;
}


-(void)viewWillAppear:(BOOL)animated{

    [self setNavigationBar];
    [self downLoadData];
    [self  initData];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    _currentPage = 1;
    
    [self createTableView];
    //    下拉刷新
    _tableV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _currentPage = 1;
        // 进入刷新状态后会自动调用这个block
        [self downLoadData];
        [_tableV.mj_header endRefreshing];
        
    }];
    //    上拉加载
    _tableV.mj_footer  = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        // 进入加载状态后会自动调用这个block
        _currentPage ++;
        NSString *pageStr  = [NSString stringWithFormat:@"%ld",_currentPage];
        
        [self downloadNextPageTableViewDataWith:pageStr];
        [_tableV.mj_footer endRefreshing];
    }];

    
    // Do any additional setup after loading the view from its nib.
}

-(void)createTableView{
    
    _tableV = [[UITableView alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT )];
    _tableV.backgroundColor  = [UIColor redColor];
    _tableV.delegate = self;
    _tableV.dataSource =self;
    _tableV.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1.0];
    [self.view addSubview:_tableV];
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    GCMLXKHTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[GCMLXKHTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    [cell setBackgroundColor:[UIColor colorWithRed:.93 green:.93 blue:.93 alpha:1]];
    //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    //    cell.textLabel.textColor = [UIColor colorWithRed:0.39 green:0.39 blue:0.39 alpha:1];
    GCMLXKHModel *listModel = _dataSource[indexPath.row];
    //NSLog(@"%@",listModel.contact);
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setListModel:listModel];
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 120;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    GCMTaskDetailViewController * taskVC = [[GCMTaskDetailViewController alloc]init];
//    GCMTaskListModel *model = _dataSource[indexPath.row];
//    taskVC.idd = model.idd;
//    [self.navigationController pushViewController:taskVC animated:YES ];
//    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
//    backItem.title = @"任务详情";
//    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
//    self.navigationItem.backBarButtonItem = backItem;
}


//初始化数据
-(void)initData{

    _dataSource = [[NSMutableArray alloc]init];
    
}

-(void)downLoadData{
    
    
    [GCMTaskHttpRequst postLXKHListDataWithtaskId:self.idd WithPage:@"1" Success:^(id obj, NSError *err) {
        NSLog(@"++--=+%@",obj);
        [_dataSource removeAllObjects];
        _dataSource = obj;
        [_tableV reloadData];
    }];
}
-(void)downloadNextPageTableViewDataWith:(NSString*)pageStr{
    //    改变当前页
    //_currentPage ++;
    [GCMTaskHttpRequst postLXKHListDataWithtaskId:self.idd WithPage:pageStr Success:^(id obj, NSError *err){
        [_dataSource addObjectsFromArray:obj];
        [_tableV reloadData];
    }];
}



//设置navigationBar
-(void)setNavigationBar{
    
    self.navigationController.navigationBarHidden = NO;
    
    self.navigationController.navigationBar.barTintColor =[UIColor colorWithRed:0.12 green:0.34 blue:0.57 alpha:1.0];
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setTitle:@"新增" forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(GotoSettings)
          forControlEvents:UIControlEventTouchUpInside];
    rightButton.frame = CGRectMake(0, 2,40, 40);
    UIBarButtonItem *menuRButton = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = menuRButton;
    
}
-(void)GotoSettings{
    
    GCMAddNewLXJLViewController *addlxkh =[[GCMAddNewLXJLViewController alloc]init];
    addlxkh.idd = self.idd;
    addlxkh.contact = self.contact;
    [self.navigationController pushViewController:addlxkh animated:YES];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    backItem.title = @"新增联系记录";
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationItem.backBarButtonItem = backItem;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
